package flm.builder;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

import flm.util.TelaUtil;

public class LeiouteCadastroBuilder {

	private Container conteiner;
	private GridBagLayout layout;
	private GridBagConstraints gridBagConstraints;
	private int colunaInicial;
	
	private int margemLateral = 25;
	private int margemVertical = 25;
	
	private Component ultimoComponenteAdicionado = null;
	
	public LeiouteCadastroBuilder(Container container) {
		this.conteiner = container;
		this.layout = new GridBagLayout();
		container.setLayout(this.layout);
		this.gridBagConstraints = new GridBagConstraints();
		this.gridBagConstraints.gridheight = 1;
		largura(1).center();
	}
	
	public LeiouteCadastroBuilder inicio(int x, int y) {
		this.colunaInicial = x;
		this.gridBagConstraints.gridx = x;
		this.gridBagConstraints.gridy = y;
		return this;
	}
	
	public LeiouteCadastroBuilder largura(int largura) {
		this.gridBagConstraints.gridheight = largura;
		return this;
	}
	
	public LeiouteCadastroBuilder esq() {
		this.gridBagConstraints.anchor = GridBagConstraints.WEST;
		return this;
	}
	
	public LeiouteCadastroBuilder dir() {
		this.gridBagConstraints.anchor = GridBagConstraints.EAST;
		return this;
	}
	
	public LeiouteCadastroBuilder center() {
		this.gridBagConstraints.anchor = GridBagConstraints.CENTER;
		return this;
	}
	
	public LeiouteCadastroBuilder add(Component comp) {
		return add(comp, 1);
	}
	
	public LeiouteCadastroBuilder add(Component comp, int largura) {
		this.gridBagConstraints.gridx += 1;
		this.gridBagConstraints.gridwidth = largura;
		this.layout.setConstraints(comp, this.gridBagConstraints);
		this.conteiner.add(comp);
		this.ultimoComponenteAdicionado = comp;
		return this;
	}
	
	public LeiouteCadastroBuilder pulaColuna() {
		this.gridBagConstraints.gridx++;
		return this;
	}
	
	public LeiouteCadastroBuilder pulaColuna(int numeroColunasPuladas) {
		this.gridBagConstraints.gridx += numeroColunasPuladas;
		return this;
	}
	
	public LeiouteCadastroBuilder novaLinha() {
		this.gridBagConstraints.gridx = this.colunaInicial;
		this.gridBagConstraints.gridy++;
		return this;
	}
	
	public LeiouteCadastroBuilder margem(int lateral, int vertical) {
		this.margemLateral = lateral;
		this.margemVertical = vertical;
		return this;
	}
	
	public GridBagLayout getLayout() {
		return layout;
	}
	
	public LeiouteCadastroBuilder trataBotao(ActionListener actionListener) {
		if (this.ultimoComponenteAdicionado != null && this.ultimoComponenteAdicionado instanceof JButton) {
			JButton button = (JButton)this.ultimoComponenteAdicionado;
			button.addActionListener(actionListener);
		}
		return this;
	}
	
	public void mostrar() {
		Window window = (Window)this.conteiner;
		if (this.conteiner instanceof Window) {
			window.pack();
		}

		this.conteiner.setSize(getLargura(), getAltura());
		
		if (this.conteiner instanceof Window) {
			TelaUtil.centralizar(window);
		}
		
		if (this.conteiner instanceof JFrame) {
			((JFrame)this.conteiner).setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		} else if (this.conteiner instanceof JDialog) {
			((JDialog)this.conteiner).setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		}
		
		this.conteiner.setVisible(true);
	}

	private int getLargura() {
		return this.conteiner.getWidth() + (2*this.margemLateral);
	}

	private int getAltura() {
		return this.conteiner.getHeight() + (2*this.margemVertical);
	}
}
